package com.xs.shop.marketing.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xs.shop.common.PageResult;
import com.xs.shop.common.Result;
import com.xs.shop.marketing.model.entity.Coupon;
import com.xs.shop.marketing.model.vo.CouponInfoVO;
import com.xs.shop.marketing.model.vo.CouponPageQueryVO;
import com.xs.shop.marketing.model.vo.PointLogListVO;
import com.xs.shop.marketing.service.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2025-07-16
 */
@RestController
@Slf4j
@RequestMapping("/coupon")
@Tag(name = "优惠券管理", description = "优惠券的CRUD操作")
public class CouponController {

    @Resource
    private CouponService couponService;

    @PostMapping("add")
    @Operation(summary = "新增优惠券", description = "创建一张新的优惠券")
    public Result<Coupon> addCoupon(@RequestBody Coupon coupon) {
        Coupon savedCoupon = couponService.addCoupon(coupon);
        return Result.success(savedCoupon);
    }

    @DeleteMapping("/normal/{id}")
    @Operation(summary = "删除优惠券", description = "根据优惠券ID删除优惠券")
    public Result<Boolean> deleteCoupon(@PathVariable("id") @Parameter(description = "优惠券ID") Long couponId) {
        boolean result = couponService.deleteCoupon(couponId);
        return Result.success(result);
    }

    @PutMapping("update")
    @Operation(summary = "更新优惠券", description = "更新优惠券信息")
    public Result<Coupon> updateCoupon(@RequestBody Coupon coupon) {
        Coupon updatedCoupon = couponService.updateCoupon(coupon);
        return Result.success(updatedCoupon);
    }

    @GetMapping("{couponId}")
    @Operation(summary = "根据ID查询优惠券", description = "根据优惠券ID查询优惠券")
    public Result<Coupon> getCouponByCouponId(@PathVariable("couponId") Long couponId) {
        Coupon coupon = couponService.getCouponByCouponId(couponId);
        return Result.success(coupon);
    }

    @GetMapping("page")
    @Operation(summary = "分页查询优惠券", description = "用户查看优惠券，分页查询")
    public PageResult<CouponPageQueryVO> page(
        @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
        @Parameter(description = "每页数量") @RequestParam(defaultValue = "10")  Integer pageSize,
        @Parameter(description = "状态 0:可用 1:已使用 2:已过期") @RequestParam(required = false) Integer status
    ) {
        log.info("优惠券查询参数：{},{},{}", current, pageSize, status);
        return PageResult.success(couponService.pageQuery(current, pageSize, status));
    }

    @DeleteMapping("{couponId}")
    @Operation(summary = "删除用户的某优惠券", description = "根据用户ID以及优惠券ID删除优惠券")
    public Result<Boolean> deleteMemberCoupon(@PathVariable("couponId") @Parameter(description = "优惠券ID") Long couponId) {
        Integer result = couponService.deleteMemberCoupon(couponId);
        return Result.success( result == 1? true: false );
    }

    @GetMapping("getCouponInfo")
    @Operation(summary = "以list形式获取某用户所拥有的可用优惠券列表", description = "根据用户ID获取可用优惠券")
    public Result<List<CouponInfoVO>> getCouponInfoList(){
        return Result.success(couponService.getCouponInfoList());
    }

    @GetMapping("checkUserHaveCoupon/{couponId}")
    @Operation(summary = "检查用户是否拥有某优惠券", description = "根据优惠券id确定用户是否持有某优惠券")
    public Result<Boolean> checkUserHaveCoupon(@PathVariable("couponId") Long couponId){
        Boolean res = couponService.checkUserHaveCoupon(couponId);
        return Result.success(res);
    }

}
